package main

import (
	"boke/blogpb"
	"context"
	"fmt"
	"google.golang.org/grpc"
)

// 发起 获取blog请求
func read(cc *grpc.ClientConn, blogid int64) *blogItem {
	// read Blog
	fmt.Println("Reading the blog")

	c := blogpb.NewBlogServiceClient(cc)

	//  测试手动指定id
	readBlogRes, err2 := c.ReadBlog(context.Background(), &blogpb.ReadBlogRequest{Blog: &blogpb.Blog{Id: 11}})
	if err2 != nil {
		fmt.Printf("Error happened while reading: %v \n", err2)
		return nil
	}
	fmt.Println("读取到的blog内容为 Blog was read: ", readBlogRes.Blog.Id, readBlogRes.Blog.Title, readBlogRes.Blog.Content, readBlogRes.Blog.AuthorId)

	// 参数查询
	readBlogReq := &blogpb.ReadBlogRequest{Blog: &blogpb.Blog{Id: blogid}}
	readBlogRes, readBlogErr := c.ReadBlog(context.Background(), readBlogReq)
	if readBlogErr != nil {
		fmt.Printf("Error happened while reading: %v \n", readBlogErr)
		return nil
	}

	b := &blogItem{
		ID:       readBlogRes.Blog.Id,
		Title:    readBlogRes.Blog.Title,
		Content:  readBlogRes.Blog.Content,
		AuthorID: readBlogRes.Blog.AuthorId,
	}

	fmt.Println("读取到的blog内容为 Blog was read: ", readBlogRes.Blog.Id, readBlogRes.Blog.Title, readBlogRes.Blog.Content, readBlogRes.Blog.AuthorId)

	return b

}
